<?php
namespace app\common\product\logic;


use app\common\BaseLogic;
use app\common\product\model\StoreSupplier;

class StoreSupplierLogic extends BaseLogic
{
    public function getStoreSupplier($id, $fields="*") {
        $cacheName = "product:supplier:{$id}";

        if ($fields == "*") {
            $data = $this->redis->hGetAll($cacheName);
        } else {
            $data = $this->redis->hMGet($cacheName, $fields);
        }

        if (empty($data['id'])) {
            $model = new StoreSupplier();
            $row = $model->where('id', $id)->find();
            if (!empty($row)) {
                $data = $row->getData();
                $this->redis->hMSet($cacheName, $data);
                if ($fields != "*") $data = array_search_keys($data, $fields);
            } else {
                $data = [];
            }
        }

        return $data;
    }

    public function resetCache($id){
        $cacheName = "product:supplier:{$id}";
        $this->redis->del($cacheName);
        $this->getStoreSupplier($id);
    }
}